//
//  ListDetailViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ListDetailViewController.h"
#import "MusicPlyController.h"
#import "ListPlayTableViewCell.h"
#import "SongListModel.h"
#import "UrlListModel.h"

@interface ListDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong) UIImageView *image;

@end

@implementation ListDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   
    
    if ([self.detailModel.songlist_id floatValue] == -1) {
        
        NSString *str = self.detailModel.link;
        UIWebView *wv = [[UIWebView alloc] initWithFrame:self.view.frame];
        
        [self.view addSubview:wv];
        //加载数据
        NSURL *url = [NSURL URLWithString:str];
        NSURLRequest *req = [NSURLRequest requestWithURL:url];
        [wv loadRequest:req];
        
    }else{
        
        self.dataArray = [NSMutableArray array];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.view addSubview:self.tableView];
        
        [self.tableView registerClass:[ListPlayTableViewCell class] forCellReuseIdentifier:@"cell"];
        self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
        self.tableView.tableHeaderView = self.image;
        [self getDataHandle];
        
    }
    
    
    
}
- (void)getDataHandle
{
    NSString *str = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100",self.songlist_id];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableArray *array = [responseObject objectForKey:@"songs"];
        for (NSDictionary *dic in array) {
            SongListModel *model = [[SongListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            
            [self.image sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"hold.png"]];
        }
       
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArray.count == 0) {
        UITableViewCell * cell  = [tableView dequeueReusableCellWithIdentifier:@"kong"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"kong"];
            
        }
        return cell;
    }
    ListPlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
       cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    SongListModel *model = self.dataArray[indexPath.row];

    cell.number.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    cell.singerLabel.text = model.name;
    cell.desLabel.text = model.singerName;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicPlyController *MusicPlay = [MusicPlyController musicplyVC];
    
    NSMutableArray *newArray = [NSMutableArray array];
    
    for (SongListModel *listModel in self.dataArray) {
        
       
        
        UrlListModel *urlModel = [listModel.urlList firstObject];
       
        
        [newArray addObject:urlModel.url];
        
    }
    
    
    MusicPlay.MusicArray = [NSMutableArray arrayWithArray:newArray];
    MusicPlay.index = indexPath.row;
    
    
    [self presentViewController:MusicPlay animated:YES completion:^{
        
        
    }];

}

@end
